/*
 * Copyright (c) 2003-2004, KNOPFLERFISH project
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following
 * conditions are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above
 *   copyright notice, this list of conditions and the following
 *   disclaimer in the documentation and/or other materials
 *   provided with the distribution.
 *
 * - Neither the name of the KNOPFLERFISH project nor the names of its
 *   contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.knopflerfish.bundle.jini;

import org.osgi.framework.Filter;
import org.osgi.framework.ServiceReference;

import org.osgi.service.jini.JiniDriver;

import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;


/**
 * DOCUMENT ME!
 *
 * @author Nico Goeminne
 */
public class Osgi2Jini {
    private static ServiceTracker serviceTracker = null;
    private static String[] cmLusExportGroups = null;

    /**
     * Creates a new Osgi2Jini object.
     *
     * @throws Exception DOCUMENT ME!
     */
    public Osgi2Jini() throws Exception {
        try {
            RMICodeBaseService.getRMICodeBaseService("/RMI");

            Filter filter = Activator.bc.createFilter("(&(" +
                    org.osgi.service.device.Constants.DEVICE_CATEGORY + "=" +
                    JiniDriver.DEVICE_CATEGORY + ")(" + JiniDriver.EXPORT +
                    "=*))");
            ServiceTrackerCustomizer serviceTrackerCustomizer = new JiniExporter();
            serviceTracker = new ServiceTracker(Activator.bc, filter,
                    serviceTrackerCustomizer);
        } catch (Exception ex) {
            serviceTracker = null;
            Debug.printDebugInfo(10,
                "Could not setup up the OSGi to Jini Bridge", ex);
            throw ex;
        }
    }

    /**
     * DOCUMENT ME!
     */
    public void open() {
        if (serviceTracker != null) {
            serviceTracker.open();
        }
    }

    /**
     * DOCUMENT ME!
     */
    public void close() {
        if (serviceTracker != null) {
            serviceTracker.close();
        }

        if (RMICodeBaseService.getRMICodeBaseService() != null) {
            RMICodeBaseService.getRMICodeBaseService().destroyService();
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @param lusExportGroups DOCUMENT ME!
     */
    public static synchronized void setCMLusExportGroups(
        String[] lusExportGroups) {
        Osgi2Jini.cmLusExportGroups = lusExportGroups;

        synchronized (serviceTracker) {
            ServiceReference[] serviceReferences = serviceTracker.getServiceReferences();
            Object[] services = serviceTracker.getServices();

            if (services != null) {
                for (int i = 0; i < services.length; i++) {
                    serviceTracker.modifiedService(serviceReferences[i],
                        services[i]);
                }
            }
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public static String[] getCMLusExportGroups() {
        return Osgi2Jini.cmLusExportGroups;
    }
}
